package com.wntms.wgj.web;

import com.github.pagehelper.PageInfo;
import com.wntms.gps.pojo.Result;
import com.wntms.wgj.pojo.DriverSettle;
import com.wntms.wgj.service.DriverSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/driverSettle")
public class DriverSettleController {

    @Autowired
    private DriverSettleService driverSettleService;

    /**
     * 查询所有司机结算信息
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<DriverSettle> list = driverSettleService.findAll();
        Result<Object> result = new Result<>();
        if (list == null){
            result.setCode(500);
            result.setMsg("查询失败");
        }else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<DriverSettle> list = driverSettleService.findAll(currentPage,pageSize);

        Result<Object> result = new Result<>();
        if (list != null){
            PageInfo<DriverSettle> pageInfo = new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    /**
     * 司机结算
     * @param driverSettle
     * @return
     */
    @PutMapping
    public Result updateSettle(@RequestBody DriverSettle driverSettle){
        boolean flag = driverSettleService.updateSettle(driverSettle);
        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("结算失败");
        }else {
            result.setCode(200);
            result.setMsg("结算成功");
        }

        return result;
    }

    /**
     * 删除司机结算记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteSettle(@PathVariable Integer id){
        boolean flag = driverSettleService.deleteSettle(id);

        Result<Object> result = new Result<>();
        if (!flag){
            result.setCode(500);
            result.setMsg("修改失败");
        }else {
            result.setCode(200);
            result.setMsg("删除成功");
        }

        return result;
    }

    /**
     * 根据运输合同cid查询单条记录
     * @param cid
     * @return
     */
    @GetMapping("/{cid}")
    public Result findByCid(@PathVariable String cid){
        DriverSettle driverSettle = driverSettleService.findByCid(cid);
        Result<Object> result = new Result<>();
        if (driverSettle != null){
            result.setCode(200);
            result.setMsg("查询单条成功");
            result.setData(driverSettle);
        }else {
            result.setCode(500);
            result.setMsg("查询单条失败");
        }

        return result;
    }
}
